<?php
namespace App\Models;

class AccountLog extends CommonModel {
	protected $table = 'shop_account_log';

    /**
     * 资金类型及其详情
     * @param $type
     * @param $amount
     * @param string $sn
     * @return mixed
     */
	private function account_type($type,$amount,$sn=''){
	    $array=array(
	        'order'=>['id'=>'1','direction'=>'1','describe'=>'因订单结算得到'.$amount.'元,订单号:'.$sn],
            'withdraw'=>['id'=>'2','direction'=>'2','describe'=>'因提现申请减少'.$amount.'元,提现单号：'.$sn],
            'split'=>['id'=>'3','direction'=>'2','describe'=>'因订单结算扣点，减少'.$amount.'元，订单号：'.$sn],
            'withdraw_fail'=>['id'=>'4','direction'=>'1','describe'=>'因提现失败退回'.$amount.'元，提现单号:'.$sn]
        );
	    return $array[$type];
    }

    /**
     * 资金记录
     * @param $uid
     * @param $amount
     * @param $type
     * @param $sn
     * @return $this|\Illuminate\Database\Eloquent\Model
     */
    public function insertAccountLog($uid,$amount,$type,$sn){
        $detail = self::account_type($type,$amount,$sn);
        $insert=array(
            'uid'=>$uid,
            'user_type'=>ClientUser::where('id','=',$uid)->value('type'),
            'sn'=>$sn,
            'account_type'=>$detail['id'],
            'account_direction'=>$detail['direction'],
            'account_describe'=>$detail['describe'],
            'amount'=>$amount,
            'account_original'=>Account::where(['uid'=>$uid])->value('amount'),
            'is_del'=>0
        );
        return self::create($insert);
    }
}